package com.frost.blog.controller;

import com.frost.blog.model.BlogInfo;
import com.frost.blog.model.UserInfo;
import com.frost.blog.service.BlogService;
import com.frost.blog.service.UserService;
import com.frost.blog.util.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.frost.blog.constants.Constant.USER_TOKEN;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 获取博客列表
     * @return
     */
    @RequestMapping("/getList")
    public List<BlogInfo> queryBlogList() {
        return blogService.queryBlogList();
    }

    /**
     * 获取博客详细信息
     * @param blogId
     * @param request
     * @return
     */
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request) {
        log.info("getBlogDetail, 接收参数: blogId: " + blogId);
        BlogInfo blogInfo = blogService.queryBlogDetail(blogId);
        String userToken = request.getHeader(USER_TOKEN);
        Integer userId = JWTUtils.getUserIdFromToken(userToken);
        if (userId != null && userId == blogInfo.getUserId()) {
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }

    /**
     * 修改博客
     * @param blogId
     * @param title
     * @param content
     * @return
     */
    @RequestMapping("/update")
    public Boolean update(Integer blogId, String title, String content) {
        log.info("update, 接收参数: title:{}, content:{}", title, content);
        if (!StringUtils.hasLength(title) || !StringUtils.hasLength(content) || blogId == null) {
            log.error("param title or content or blogId is null");
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setTitle(title);
        blogInfo.setContent(content);
        Integer result = blogService.updateBlog(blogInfo);
        if (result < 1) {
            log.error("博客更新失败");
            return false;
        }
        return true;
    }

    /**
     * 删除博客
     * @param blogId
     * @return
     */
    @RequestMapping("/delete")
    public Boolean delete(Integer blogId) {
        log.info("delete, 接收参数: blogId:{}", blogId);
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        Integer result = blogService.updateBlog(blogInfo);
        if (result < 1) {
            log.error("博客删除失败");
            return false;
        }
        return true;
    }

    /**
     * 添加博客
     * @param title
     * @param content
     * @param request
     * @return
     */
    @RequestMapping("/add")
    public Boolean publishBlog(String title, String content, HttpServletRequest request) {
        log.info("publishBlog, 接收参数: title:{}, content:{}", title, content);
        /**
         * 1、参数校验
         * 2、获取当前登录用户
         * 3、博客发布
         */
        if (!StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            log.error("param title or content is null");
            return false;
        }
        String userToken = request.getHeader(USER_TOKEN);
        Integer userId = JWTUtils.getUserIdFromToken(userToken);
        // 根据id获取用户信息
        if (userId == null || userId <= 0) {
            log.error("用户未登录");
            return false;
        }
        BlogInfo blogInfo = new BlogInfo(title, content, userId);
        Integer result = blogService.publishBlog(blogInfo);
        if (result < 1) {
            log.error("博客发布失败");
            return false;
        }
        return true;
    }

}
